import index from '../components/index.vue'
import login from '../components/login/Login.vue'
import user from '../components/user/user.vue'
import wuhan from '../components/echarts/wuhan.vue'
import group from '../components/group/group.vue'
import msg from '../components/msg/msg.vue'
import VueRouter from 'vue-router'
import Vue from 'vue'
//引入js-cookie
import Cookies from 'js-cookie'

Vue.use(VueRouter)


 const routes = [
    { path: '/home', component: index ,
        children: [
            {
            path: 'user',
            component: user
            },
            {
              path: 'group',
              component: group
            },
            {
              path: 'msg',
              component: msg
            }                     
        ]},
    { path: '/login', component: login },
    // 这个是专门显示武汉疫情的官方数据
    { path: '/wuhan', component: wuhan },
    { path: '*', redirect: '/login' }
  ]
const router = new VueRouter({
    //美化url需要nginx等服务器做相应的特殊设置
    // mode:'history',
    routes // (缩写) 相当于 routes: routes
  })

  //全局前置守卫
  router.beforeEach((to,from,next) => {
    var loginName = Cookies.get("loginName");
    //避免登入界面一直循环
    if(to.path == "/login" || to.path == "/wuhan"){
      next();
    }else if(loginName){
      next();
    }else{
      next({
        path:'/login',
        query:{redirect:to.fullPath}
      })
    }
  })
  export default router;

